from indicators import idx
import pandas as pd
import csv
from itertools import islice
import numpy as np
from fastpip import pip


def main():
    input_file = open("../data_origin/data.csv")
    csv_data = []
    for line in islice(input_file, 1, None):
        csv_data.append(line)

    csv_data_len  = len(csv_data)
    csv_data = csv_data[::-1]

    # 创建报告report
    report_csv = open('../data_report/report-ml.csv', 'w', newline='')
    writer = csv.writer(report_csv)
    writer.writerow([
                    'date',
                    'open_price',
                    'high_price',
                    'low_price',
                    'close_price',
                    'vol',
                    'today_pema_6',
                    'today_pema_18',
                    'today_pema_54',
                    'pclose6',
                    'pclose18',
                    'pclose54',
                    'today_vema_5',
                    'today_vema_15',
                    'today_vema_45',
                    'p_angle_pema6',
                    'p_angle_pema18',
                    'p_angle_pema54',
                    'v_angle_vema5',
                    'v_angle_vema15',
                    'v_angle_vema45',
                    'tomorrowClose'
                     ])
    report_list = []

    last_pema_6 = 0
    last_pema_18 = 0
    last_pema_54 = 0
    last_vema_5 = 0
    last_vema_15 = 0
    last_vema_45 = 0

    Statistics = []
    priceRangePool = [] # 最近的价格池
    volumRangePool = [] # 最近的成交量池
    priceAngleRangePool = [] # 最近的价格角度池
    volumAngelRangePool = [] # 最近的成交量角度池
    candleRangePool = [] # 蜡烛图范围池
    candleStatusRangePool = [] # 蜡烛图状态范伟池
    candleMRangePool = []  # 蜡烛图M状态范伟池

    close_price_list = []
    for i,line_data in enumerate(csv_data):
        datas = line_data.split(',')
        close_price = float(datas[4])
        close_price_list.append((i, close_price))
    # 先计算下pip列表

    pip_num = int(csv_data_len / 3.14)

    pip_list = pip(close_price_list, pip_num)

    pip_map = dict(pip_list)

    for i,line_data in enumerate(csv_data):

        valid = False # 本行数据是否有效
        # 价格相关
        datas = line_data.split(',')
        date = datas[0]
        open_price = float(datas[1])
        high_price = float(datas[2])
        low_price = float(datas[3])
        close_price = float(datas[4])
        volume = int(datas[5])



        today_pema_6 = idx.ema6(last_pema_6, close_price)
        today_pema_18 = idx.ema18(last_pema_18, close_price)
        today_pema_54 = idx.ema54(last_pema_54, close_price)


        pclose6 = idx.close(close_price, today_pema_6)
        pclose18 = idx.close(close_price, today_pema_18)
        pclose54 = idx.close(close_price, today_pema_54)
        price_distance = idx.pipsort([pclose6, pclose18, pclose54, 0])
        price_distance_str = "_".join([str(i) for i in price_distance])

        # 成交量相关

        today_vema_5 = idx.vema5(last_vema_5, volume)
        today_vema_15 = idx.vema15(last_vema_15, volume)
        today_vema_45 = idx.vema45(last_vema_45, volume)

        vclose5 = idx.close(volume, today_vema_5)
        vclose15 = idx.close(volume, today_vema_15)
        vclose45 = idx.close(volume, today_vema_45)

        vol_distance = idx.pipsort([vclose5, vclose15, vclose45, 0])
        vol_distance_str = "_".join([str(i) for i in vol_distance])

        # 价格角度相关
        p_angle_pema6 = idx.angle_ema(today_pema_6,last_pema_6)
        p_angle_pema18 = idx.angle_ema(today_pema_18,last_pema_18)
        p_angle_pema54 = idx.angle_ema(today_pema_54,last_pema_54)

        p_angle_list =  idx.pipsort([p_angle_pema6, p_angle_pema18, p_angle_pema54, 0])
        p_angle_list_str = "_".join([str(i) for i in p_angle_list])

        # 成交量角度相关
        v_angle_vema5 = idx.angle_ema(today_vema_5, last_vema_5)
        v_angle_vema15 = idx.angle_ema(today_vema_15, last_vema_15)
        v_angle_vema45 = idx.angle_ema(today_vema_45, last_vema_45)

        v_angle_list = idx.pipsort([v_angle_vema5, v_angle_vema15, v_angle_vema45, 0])
        v_angle_list_str = "_".join([str(i) for i in v_angle_list])

        # SMA周期
        WeightPeriods = 15
        SegmentCount = 3

        # PriceSignature计算

        price_signature_list_str = idx.calSignature(pclose6,pclose18,pclose54,priceRangePool)

        # VolumSignature计算
        vol_signature_list_str = idx.calSignature(vclose5, vclose15, vclose45,volumRangePool)

        # 价格角度signature
        price_angle_signature = idx.calSignature(p_angle_pema6, p_angle_pema18, p_angle_pema54,priceAngleRangePool)

        # 成交量角度signature
        vol_angle_signature = idx.calSignature(v_angle_vema5, v_angle_vema15, v_angle_vema45,volumAngelRangePool)

        # Candle计算
        candle_signature = idx.calCandleSignature(open_price,high_price,low_price,close_price,candleRangePool)

        #Candle Status计算
        candle_status_signature = idx.calCandleStatusSignature(i, csv_data, open_price, high_price, close_price, candleStatusRangePool)

        #CandlesM 计算
        candle_M_signature = idx.calCandleMSignature(i, csv_data, open_price, high_price, low_price, close_price, candleMRangePool)


        #pip
        pipPoint = ""
        if i in pip_map:
            pipPoint = str(pip_map[i])

        # 计算第二天是涨还是跌

        tomorrow_close = close_price
        tomorrow_status = 0
        if i+1 < csv_data_len:
            tomorrow_close = csv_data[i + 1].split(',')[4]
            if float(tomorrow_close) > close_price:
                tomorrow_status = 1
            else:
                tomorrow_status= -1


        if not vol_signature_list_str  is None and not price_signature_list_str is None:
            # 加入汇总报告
            report_list.append([
                                date,
                                open_price,
                                high_price,
                                low_price,
                                close_price,
                                volume,
                                '%.2f' % today_pema_6,
                                '%.2f' % today_pema_18,
                                '%.2f' % today_pema_54,
                                '%.2f' % pclose6,
                                '%.2f' % pclose18,
                                '%.2f' % pclose54,
                                '%.2f' % today_vema_5,
                                '%.2f' % today_vema_15,
                                '%.2f' % today_vema_45,
                                '%.2f' % p_angle_pema6,
                                '%.2f' % p_angle_pema18,
                                '%.2f' % p_angle_pema54,
                                '%.2f' % v_angle_vema5,
                                '%.2f' % v_angle_vema15,
                                '%.2f' % v_angle_vema45,
                                tomorrow_close
                                ])

        last_pema_6 = today_pema_6
        last_pema_18 = today_pema_18
        last_pema_54 = today_pema_54

        last_vema_5 = today_vema_5
        last_vema_15 = today_vema_15
        last_vema_45 = today_vema_45




    print('1',report_list[::-1][0])
    print('2',report_list[0])
    writer.writerows(report_list[::-1])  # 倒序输出


    # from collections import Counter
    # print(Counter(Statistics))


if __name__ == '__main__':
    main()
